#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        unordered_map<int,int> m;
        int res = 0;
        int left = 0;
        for(int i = 0; i < fruits.size(); ++i){
            m[fruits[i]] ++;
            while(m.size() > 2){
                if(--m[fruits[left]] == 0){ // 滑动窗口，让第一种出窗口
                    m.erase(fruits[left]);
                }
                left ++;
            }
            res = max(res,i - left + 1);
        }
        return res;
    }
};